package ru.amse.baltijsky.javascheme.nodeshape;

import ru.amse.baltijsky.javascheme.util.Dir;
import ru.amse.baltijsky.javascheme.util.Size;
import ru.amse.baltijsky.javascheme.util.VisConst;

import java.awt.*;

/**
 * A convenience superclass for branch shapes.
 */
public abstract class BranchShape extends AbstractNodeShape {
    /**
     * Offset for the text inside the text frame.
     */
    private static final Size OFFSET = new Size(5, 5);
    /**
     * Size of the branch shape bottom (set by the user).
     */
    private int bottomSize;

    /**
     * Creates a branch shape with the specified code.
     *
     * @param code the code for the shape
     */
    public BranchShape(String code) {
        super(code);
        bottomSize = VisConst.YGAP;
    }

    public Size getBoundRectSize(Graphics g) {
        Size codeSize = getCodeSize(g);
        int x = OFFSET.x + codeSize.x + OFFSET.x;
        int y = bottomSize + OFFSET.y + codeSize.y + OFFSET.y;
        return new Size(x, y);
    }

    @Override
    public int[] getShapeDims(Graphics g) {
        int[] ret = new int[Dir.length()];
        Size codeSize = getCodeSize(g);
        ret[Dir.l()] = OFFSET.x + codeSize.x / 2;
        ret[Dir.r()] = codeSize.x / 2 + OFFSET.x;
        ret[Dir.u()] = (codeSize.x == 0) ? 0 : (OFFSET.y + codeSize.y + OFFSET.y); // codeSize.y != 0, it's font height
        ret[Dir.d()] = bottomSize;
        return ret;
    }

    /**
     * Returns the text offset in the text frame (internal interface).
     *
     * @return offset of the text in the text frame
     */
    protected static Size getOffset() {
        return OFFSET;
    }

    /**
     * Returns the bottom size of the branch shape (internal interface).
     *
     * @return bottom size
     */
    protected int getBottomSize() {
        return bottomSize;
    }

    /**
     * Sets the bottom size for the shape.
     *
     * @param bottomSize the desired bottom size
     */
    public void setBottomSize(int bottomSize) {
        this.bottomSize = bottomSize;
    }
}
